package co.dreamapps.dodoattack.model;

import java.io.IOException;

import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.engine.Engine;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.TextureManager;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.modifier.IModifier;

import co.dreamapps.dodoattack.activities.GameActivity;
import android.content.Context;
import android.util.Log;

public class Corn {

	/**
	 * Constants
	 */
	private static final double COLLIDE_DISTANCE = 90;

	/**
	 * Resources
	 */
	private static ITextureRegion texturaMaiz;
	private static Sound cornSound;

	/**
	 * Attributes
	 */
	private Sprite spriteMaiz;
	private GameActivity game;
	private GameState gameState;
	private int aimedX, aimedY;
	private boolean collided = false;

	/**
	 * Modifiers
	 */
	private LoopEntityModifier loopModifier;
	private MoveModifier moveModifier;

	public Corn(GameActivity game, GameState gameState) {
		this.game = game;
		this.gameState = gameState;
	}

	public static void onCreateResources(TextureManager textureManager, Engine engine, Context context) throws IOException {

		// Image
		BitmapTextureAtlas atlasMoneda = new BitmapTextureAtlas(textureManager, 55, 55,TextureOptions.DEFAULT);
		texturaMaiz  = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlasMoneda, context, "corn.png", 0, 0);		
		atlasMoneda.load();

		// Sound
		try {			 
			cornSound = SoundFactory.createSoundFromAsset(engine.getSoundManager(), context, "sonido/maiz.mp3");
			cornSound.setLooping(false);
		} catch (IOException e) {}
	}

	public void addCornOnScene(VertexBufferObjectManager vertexBufferObjectManager, final Scene scene, int x, int y, final int aimedX, final int aimedY, final IEntity dodoEntity) {		
		spriteMaiz = new Sprite(x, y, texturaMaiz, vertexBufferObjectManager) {
			@Override
			protected void onManagedUpdate(float pSecondsElapsed) { 
				if(!collided) {
					float myX = spriteMaiz.getX();
					float myY = spriteMaiz.getY();
					float eggX = dodoEntity.getX();
					float eggY = dodoEntity.getY();
					if(Math.abs(Math.sqrt(Math.pow(myX-eggX, 2) + Math.pow(myY-eggY, 2))) < COLLIDE_DISTANCE) {
						collided = true;
						spriteMaiz.unregisterEntityModifier(loopModifier);
						spriteMaiz.unregisterEntityModifier(moveModifier);
						spriteMaiz.registerEntityModifier(new MoveModifier(1f, spriteMaiz.getX(), spriteMaiz.getY(), aimedX, aimedY, new IEntityModifierListener() {
							@Override
							public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
								cornSound.play();
							}

							@Override
							public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
								spriteMaiz.setVisible(false);
								game.removeEntitiy(scene, spriteMaiz);
								gameState.increaseCorn();
							}
						}));
					}
				}
				super.onManagedUpdate(pSecondsElapsed);
			} 
		};
		scene.attachChild(spriteMaiz);
		loopModifier = new LoopEntityModifier(new SequenceEntityModifier(new ScaleModifier(0.6f, 1, 1.5f), new ScaleModifier(0.6f, 1.5f, 1)));
		moveModifier = new MoveModifier(4f, x, y, -100, y);
		spriteMaiz.registerEntityModifier(loopModifier);
		spriteMaiz.registerEntityModifier(moveModifier);
	}

}
